﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
/// <summary>
/// Summary description for WebServiceDRM
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService]
public class WebServiceDRM : System.Web.Services.WebService {

    static ClassExecute.ClassExecute dt;
    static string str = webapp4U.Configuration.CurrentConnectionString;
    static string error = "";
    public WebServiceDRM () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public string HelloWorld() {
        return "Hello World";
    }
     [WebMethod(EnableSession = true)]
    public string[] GetMatHangInfo(string prefixText, int count)
    {
        dt = new ClassExecute.ClassExecute();
        try
        {
            if (count == 0)
            {
                count = 10;
            }
            DataTable db = dt.Get_Data_DataTable(str, ref error, "k_getFilterListMatHang", CommandType.StoredProcedure,
                new SqlParameter("@filter",prefixText),
                new SqlParameter("@Sitecode",HttpContext.Current.Session["Sitecode"].ToString()));

            List<string> items = new List<string>(count);
            for (int i = 0; i < db.Rows.Count; i++)
            {
                string strName = AjaxControlToolkit.AutoCompleteExtender.CreateAutoCompleteItem(db.Rows[i]["MaTenHang"].ToString(), db.Rows[i]["MaTenHang"].ToString());
                items.Add(strName);
            }

            return items.ToArray();
        }
        catch
        {
            return new string[] { "Không có dữ liệu" };
        }
    }

    
     [WebMethod(EnableSession = true)]
     public string[] GetMatHangInfoFimex(string prefixText, int count)
     {
         dt = new ClassExecute.ClassExecute();
         try
         {
             if (count == 0)
             {
                 count = 10;
             }
             DataTable db = dt.Get_Data_DataTable(str, ref error, "TTr_getFilterListMatHangFi", CommandType.StoredProcedure,
                 new SqlParameter("@filter", prefixText),
                 new SqlParameter("@Sitecode", HttpContext.Current.Session["Sitecode"].ToString()));

             List<string> items = new List<string>(count);
             for (int i = 0; i < db.Rows.Count; i++)
             {
                 string strName = AjaxControlToolkit.AutoCompleteExtender.CreateAutoCompleteItem(db.Rows[i]["MaTenHang"].ToString(), db.Rows[i]["MaTenHang"].ToString());
                 items.Add(strName);
             }

             return items.ToArray();
         }
         catch
         {
             return new string[] { "Không có dữ liệu" };
         }
     }

     [WebMethod(EnableSession = true)]
     public string[] GetQuyCachProductFimex(string prefixText, int count)
     {
         dt = new ClassExecute.ClassExecute();
         try
         {
             if (count == 0)
             {
                 count = 10;
             }
             DataTable db = dt.Get_Data_DataTable(str, ref error, "TTr_getQuyCachProductInforToGetFromNCCFi", CommandType.StoredProcedure,
                 new SqlParameter("@filter", prefixText),
                 new SqlParameter("@Sitecode", HttpContext.Current.Session["Sitecode"].ToString()));

             List<string> items = new List<string>(count);
             for (int i = 0; i < db.Rows.Count; i++)
             {
                 string strName = AjaxControlToolkit.AutoCompleteExtender.CreateAutoCompleteItem(db.Rows[i]["QuyCach"].ToString(), db.Rows[i]["QuyCach"].ToString());
                 items.Add(strName);
             }

             return items.ToArray();
         }
         catch
         {
             return new string[] { "Không có dữ liệu" };
         }
     }


    [WebMethod(EnableSession = true)]
     public string[] GetMatHangInfoWithContextKey(string prefixText, int count, string contextKey)
     {
         dt = new ClassExecute.ClassExecute();
         try
         {
             if (count == 0)
             {
                 count = 10;
             }
             DataTable db = dt.Get_Data_DataTable(str, ref error, "TTr_getFilterProductInfoToGetFromNCC", CommandType.StoredProcedure,
                 new SqlParameter("@filter", prefixText),
                 new SqlParameter("@Sitecode", HttpContext.Current.Session["Sitecode"].ToString()),
                 new SqlParameter("@vendorkey", contextKey));

             List<string> items = new List<string>(count);
             for (int i = 0; i < db.Rows.Count; i++)
             {
                 string strName = AjaxControlToolkit.AutoCompleteExtender.CreateAutoCompleteItem(db.Rows[i]["MaTenHang"].ToString(), db.Rows[i]["MaTenHang"].ToString());
                 items.Add(strName);
             }

             return items.ToArray();
         }
         catch
         {
             return new string[] { "Không có dữ liệu" };
         }
     }

    [WebMethod]
    public string[] GetNPPInfo(string prefixText, int count)
    {
        dt = new ClassExecute.ClassExecute();
        try
        {
            if (count == 0)
            {
                count = 10;
            }
            DataTable db = dt.Get_Data_DataTable(str, ref error, "k_getFilterListNPP", CommandType.StoredProcedure,
                new SqlParameter("@filter", prefixText));

            List<string> items = new List<string>(count);
            for (int i = 0; i < db.Rows.Count; i++)
            {
                string strName = AjaxControlToolkit.AutoCompleteExtender.CreateAutoCompleteItem(db.Rows[i]["MaTenNPP"].ToString(), db.Rows[i]["MaTenNPP"].ToString());
                items.Add(strName);
            }

            return items.ToArray();
        }
        catch
        {
            return new string[] { "Không có dữ liệu" };
        }
    }

    //Filter Customer TDV
    [WebMethod(EnableSession = true)]
    public string[] GetFilterTDV(string prefixText, int count)
    {
        dt = new ClassExecute.ClassExecute();
        try
        {
            if (count == 0)
            {
                count = 10;
            }
            DataTable db = dt.Get_Data_DataTable(str, ref error, "[k_getFilterListTDV]", CommandType.StoredProcedure,
                new SqlParameter("@filter", prefixText),
                new SqlParameter("@Adncode",HttpContext.Current.Session["Adncode"].ToString()));

            List<string> items = new List<string>(count);
            for (int i = 0; i < db.Rows.Count; i++)
            {
                string strName = AjaxControlToolkit.AutoCompleteExtender.CreateAutoCompleteItem(db.Rows[i]["TDV"].ToString(), db.Rows[i]["TDV"].ToString());
                items.Add(strName);
            }

            return items.ToArray();
        }
        catch
        {
            return new string[] { "Không có dữ liệu" };
        }
    }

    //Filter Customer DKSH
    [WebMethod(EnableSession = true)]
    public string[] GetFilterCustomerInfoDKSH(string prefixText, int count)
    {
        dt = new ClassExecute.ClassExecute();
        try
        {
            if (count == 0)
            {
                count = 10;
            }
            DataTable db = dt.Get_Data_DataTable(str, ref error, "TTr_getFilterListCustomer", CommandType.StoredProcedure,
                new SqlParameter("@filter", prefixText));

            List<string> items = new List<string>(count);
            for (int i = 0; i < db.Rows.Count; i++)
            {
                string strName = AjaxControlToolkit.AutoCompleteExtender.CreateAutoCompleteItem(db.Rows[i]["MaTenKH"].ToString(), db.Rows[i]["MaTenKH"].ToString());
                items.Add(strName);
            }

            return items.ToArray();
        }
        catch
        {
            return new string[] { "Không có dữ liệu" };
        }
    }
    //Filter Customer DKSH
    [WebMethod(EnableSession = true)]
    public string[] GetFilterCustomerInfoNMD(string prefixText, int count)
    {
        dt = new ClassExecute.ClassExecute();
        try
        {
            if (count == 0)
            {
                count = 10;
            }
            DataTable db = dt.Get_Data_DataTable(str, ref error, "TTr_getFilterListCustomerNMD", CommandType.StoredProcedure,
                new SqlParameter("@filter", prefixText));

            List<string> items = new List<string>(count);
            for (int i = 0; i < db.Rows.Count; i++)
            {
                string strName = AjaxControlToolkit.AutoCompleteExtender.CreateAutoCompleteItem(db.Rows[i]["MaTenKH"].ToString(), db.Rows[i]["MaTenKH"].ToString());
                items.Add(strName);
            }

            return items.ToArray();
        }
        catch
        {
            return new string[] { "Không có dữ liệu" };
        }
    }

    //Filter Product DKSH
    [WebMethod(EnableSession = true)]
    public string[] GetFilterProductInfoDKSH(string prefixText, int count)
    {
        dt = new ClassExecute.ClassExecute();
        try
        {
            if (count == 0)
            {
                count = 10;
            }
            DataTable db = dt.Get_Data_DataTable(str, ref error, "[k_getFilterListMatHangDKSH]", CommandType.StoredProcedure,
                new SqlParameter("@filter", prefixText));

            List<string> items = new List<string>(count);
            for (int i = 0; i < db.Rows.Count; i++)
            {
                string strName = AjaxControlToolkit.AutoCompleteExtender.CreateAutoCompleteItem(db.Rows[i]["MaTenHang"].ToString(), db.Rows[i]["MaTenHang"].ToString());
                items.Add(strName);
            }

            return items.ToArray();
        }
        catch
        {
            return new string[] { "Không có dữ liệu" };
        }
    }
    //Filter Product NMD
    [WebMethod(EnableSession = true)]
    public string[] GetFilterProductInfoNMD(string prefixText, int count)
    {
        dt = new ClassExecute.ClassExecute();
        try
        {
            if (count == 0)
            {
                count = 10;
            }
            DataTable db = dt.Get_Data_DataTable(str, ref error, "[k_getFilterListMatHangNMD]", CommandType.StoredProcedure,
                new SqlParameter("@filter", prefixText));

            List<string> items = new List<string>(count);
            for (int i = 0; i < db.Rows.Count; i++)
            {
                string strName = AjaxControlToolkit.AutoCompleteExtender.CreateAutoCompleteItem(db.Rows[i]["MaTenHang"].ToString(), db.Rows[i]["MaTenHang"].ToString());
                items.Add(strName);
            }

            return items.ToArray();
        }
        catch
        {
            return new string[] { "Không có dữ liệu" };
        }
    }

    [WebMethod(EnableSession = true)]
    public string GetWorkingDay()
    {
        //string sdate1 = HttpContext.Current.Session["workingday"].ToString();
        //DateTime date1 = new DateTime(int.Parse(sdate1.Substring(6, 4)), int.Parse(sdate1.Substring(3, 2)), int.Parse(sdate1.Substring(0, 2)));
        //string date2 = date1.AddDays(-1).ToString("dd/MM/yyyy");
        return "01/" + DateTime.Now.Month + "/" + DateTime.Now.Year;
    }

}
